#include "stdafx.h"
#include "XLib.h"

#include "XThread.h"

#ifdef WIN32
#else
unsigned long GetTickCount()
{
	struct timespec ts;
	clock_gettime(CLOCK_MONOTONIC, &ts);
	return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}
#endif//

long XLibInit::s_bInit = 0;

void XLibInit::Init()
{
#ifdef WIN32
	long result = InterlockedIncrement((LONG*)&s_bInit);
#else
	long result = ++s_bInit;
#endif//
	if (1==result) {
#if USE_THREADPOOL
		StartThreadPool();
#endif//USE_THREADPOOL
	}
}

void XLibInit::Release()
{
#ifdef WIN32
	long result = InterlockedDecrement((LONG*)&s_bInit);
#else
	long result = --s_bInit;
#endif//
	if (0==result) {
#if USE_THREADPOOL
		StopThreadPool();
#endif//USE_THREADPOOL
	}
}

XLibInit::XLibInit()
{
	Init();
}

XLibInit::~XLibInit()
{
	Release();
}